// pages/income/withDraw/history/history.js

import {
  getWXIncome
} from '../../../../api/income';
import {
  getRandom
} from '../../../../utils/index'
import {
  format
} from '../../../../utils/date'

import {
  WITHDRAW_TYPE,
  TYPE,
  HINT,
  SHOW_COUNT
} from '../../../../config'
let loading = false;
let currentPage = 1;
let totalPage = 1;
let showCount = SHOW_COUNT

Page({

  /**
   * 页面的初始数据
   */
  data: {
    withdraw_history: [

    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    loading = false;
    currentPage = 1;
    totalPage = 1;
    showCount = SHOW_COUNT
    this.initWithDrawList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.initWithDrawList();

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

    if (currentPage > totalPage) {
      wx.showToast({
        title: '已经到底了',
        icon: 'none'
      })
      return;
    }

    this.initWithDrawList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // methods 

  initWithDrawList() {
    if (loading) {
      return;
    }
    wx.showLoading({
      title: '加载中'
    })
    loading = true;
    getWXIncome({
        showCount: showCount,
        currentPage: currentPage
      })
      .then(res => {
 
        let list = res.data.list
        if (list) {
          
          for (let i = 0; i < list.length; i++) {
            let type = getRandom(1, 4)
            if(list[i].info){
              list[i].hint = list[i].info.context
            }
            else{
              list[i].hint = '嗨喵悦动'
            }
            
            // list[i].hint =  list[i].info.context.replace('嗨喵悦动-','')
            list[i].money = list[i].redsum
            list[i].date = format(list[i].finish_time).substr(0, 16),
              list[i].type = type
            list[i].withdraw_type = TYPE[getRandom(1, 3)]

          }
          let pre_list = this.data.withdraw_history
          
          
          this.setData({
            withdraw_history: pre_list.concat(list),
          })
          currentPage++

        }
        if (res.data.totalPage) {

          totalPage = res.data.totalPage
        }


        loading = false;
        wx.hideLoading()

      })
      .catch(err => {
        
        loading = false;
        wx.hideLoading()

      })

  }


})